package bcxd.firstline.focalcompany.machining;

import java.sql.Connection;
import java.util.Map;

import org.limp.basework.MachiningException;
import org.limp.basework.SimpleBean;
import org.limp.basework.SimpleBean4Table;
import org.limp.basework.SimpleBeanMachining;
import org.limp.basework.Table;
import org.limp.basework.impl.CommonBean4HSQ;
import org.limp.basework.impl.SimpleBean4TableImpl;
import org.limp.basework.tools.BaseworkUtil;
import org.limp.mine.DataTrimmerI;
import org.limp.mine.EncryptTool;
import org.limp.mine.RandomUtil;
import org.limp.mine.StringTool;

/**
 * 核心企业入驻申请数据加工
 * 
 * @author mido
 * 
 */
public class FocalCompanyAuditMachining implements SimpleBeanMachining {
	/**
	 * 
	 */
	public SimpleBean machining(SimpleBean arg0, Table arg1,
			CommonBean4HSQ arg2, Connection arg3, int arg4)
			throws MachiningException {
		// TODO Auto-generated method stub
		if (arg4 == 1) {
			String pkid = StringTool
					.checkString(arg0.getResource().get("pkid"));
			String status = StringTool.checkString(arg0.getResource().get(
					"fcauditstatus"));

			BaseworkUtil util = new BaseworkUtil();

			String sql = "SELECT ID FROM BCXD_USER WHERE UTYPE = 'focalCompany' AND UTYPEID = ?";
			DataTrimmerI trimmer = new DataTrimmerI(arg3);
			Map temp = trimmer.searchSingleData(sql, pkid);

			if (temp == null) {
				SimpleBean4Table bxcdUserInfo = new SimpleBean4TableImpl(
						"BCXD_USER");
				bxcdUserInfo.getResource().put("uname",
						arg0.getResource().get("fcname"));
				bxcdUserInfo.getResource().put("logid",
						RandomUtil.getRandomString(10));
				bxcdUserInfo.getResource().put("pwd",
						EncryptTool.encryptString("000000"));
				bxcdUserInfo.getResource().put("utype", "focalCompany");
				bxcdUserInfo.getResource().put("ustatus", status);

				try {
					util.manualSave1(bxcdUserInfo, arg3);
				} catch (Exception ex) {
					throw new MachiningException(
							"create focal-company user error.", ex);
				}
			} else {
				SimpleBean4Table bxcdUserInfo = new SimpleBean4TableImpl(
						"BCXD_USER");
				bxcdUserInfo.getResource().put("uid", temp.get("ID"));
				bxcdUserInfo.getResource().put("ustatus", status);

				try {
					util.manualUpdate1(bxcdUserInfo, arg3);
				} catch (Exception ex) {
					throw new MachiningException(
							"audit focal-company user error.", ex);
				}
			}

		}
		return null;
	}
}
